<?php
Class Data_Team extends Data_Base{

    /**
	 * 表名
	 *
	 * @var string
	 */
    static $table = 'team';
    /**
	 * 主键
	 * 
	 * @var  int
	 */
    static $primaryKey = 'team_id';
    /**
	 * 软删除标记
	 *
	 * @var int
	 */
    static $delflag = 'is_del';

    /**
	 * 构造函数
	 * 
	 * @access public
	 * @return void
	 */
    public function __construct() {
        parent::__construct ();
        parent::$table = self::$table;
        parent::$primaryKey = self::$primaryKey;
    }

    //获得全部队伍数据
    function getAllTeam(){
        $sql	= "SELECT * FROM `Team` ";
        $datas	= $this->get_sql($sql);
        return $datas;
    }

    //获得全部队伍数据
    function getTeam($team_id,$season){
        $sql	= sprintf("SELECT * FROM `Team` WHERE `team_id`='%s' AND season = '%s'",$team_id,$season);
        $datas	= $this->get_sql($sql);
        return $datas;
    }
    //获得全部队伍短名称数据
    function getAllTeamShort(){
        $sql	= sprintf("SELECT * FROM `team_short`");
        $datas	= $this->get_sql($sql);
        return $datas;
    }
    //获得全部队伍数据
    function getTeamStatsSeasonRound($season,$round){
        $sql	= sprintf("SELECT g.season,g.round,g.game_id,g.h_total,g.v_total,g.v_team_name,g.h_team_name,s.h_team_id*1 as h_team_id,s.v_team_id*1 as v_team_id FROM `game_data` as g,`schedule` as s WHERE g.`round`*1='%s' AND g.season*1 = '%s' AND s.`round`*1='%s' AND s.season*1 = '%s' AND g.game_id = s.game_id",$round,$season,$round,$season);
        $datas	= $this->get_sql($sql);
        return $datas;
    }
    //获得球队某轮次红黄牌数
    function getTeamStatsCard($season,$game_id,$team){
        $sql	= sprintf("SELECT sum(if(1st_yellow='',0,1)+if(2nd_yellow='',0,1)) as yellow,sum(if(red='',0,1)) as red FROM `game_player` WHERE season = %d AND game_id = '%s' AND team_id*1 = '%d'",$season,$game_id,$team);
        $datas	= $this->get_sql($sql);
        return $datas;
    }
    //获得减分球队
    function getDescoreTeam($season){
        $sql	= sprintf("SELECT * FROM `team_descore` WHERE season = '%s'",$season);
        $datas	= $this->get_sql($sql);
        return $datas;
    }
    //更新球队积分榜
    function replaceRank($data){
        foreach ($data as $key=>$per){
            $set .= sprintf(" `%s`='%s' ,",$key,$per);
        }
        $set = trim($set,',');
        $sql = sprintf("REPLACE INTO `team_rank` SET %s",$set);
        $datas = $this->insert_sql($sql);
        if($datas['errorcode']) exit($sql."\r\n this sql error!!!!\r\n");
        return $datas;
    }
    //获取积分榜
    function getRank($season,$round){
        $sql	= sprintf("SELECT team_name_s,season,team_id,team_name_l,goal,be_goaled,win,draw,lose,score,rank,torank FROM `team_rank` WHERE season = '%s' AND round='%s' ORDER BY rank ASC",$season,$round);
        $datas	= $this->get_sql($sql);
        return $datas;
    }
    //获取球队积分榜
    function getTeamRankInfo($season,$round,$team_id){
        $sql	= sprintf("SELECT team_name_s,season,team_id,team_name_l,goal,be_goaled,win,draw,lose,score,rank,torank,yellow,red FROM `team_rank` WHERE season = '%s' AND round='%s' AND team_id='%d' limit 1",$season,$round,$team_id);
        $datas	= $this->get_sql($sql);
        return $datas;
    }
}